<script setup>
import { useRouter } from 'vue-router';
import { ref, onMounted } from 'vue';
import { Shop} from '@element-plus/icons-vue'; // 安装@element-plus/icons-vue
import axios from 'axios';
import { ElMessage } from 'element-plus'; // 引入消息提示
const Router = useRouter();
const confirm = () => {
  console.log("退出登录");
  // 退出登录业务实现
  Router.replace({ path: '/' });
};

const seller = ref({
  sellerId: null,
  sellerName: '',
  avatarUrl: '',
  storeName: '' // 确保包含商店名称
});

const fetchSellerInfo = async () => {
  try {
    const response = await axios.get('http://localhost:8080/sellers/1');
    seller.value = response.data;
  } catch (error) {
    console.error('获取卖家信息失败:', error);
  }
};

// 修改密码对话框状态
const isEditingPassword = ref(false);
const username = ref(''); // 新增用户名输入框的绑定
const currentPassword = ref('');
const newPassword = ref('');
const confirmPassword = ref('');

const openPasswordDialog = () => {
  isEditingPassword.value = true;
};


onMounted(fetchSellerInfo);


const onSavePassword = async () => {
  if (newPassword.value !== confirmPassword.value) {
    ElMessage.error('新密码与确认密码不一致！');
    return;
  }

  try {
    const response = await axios.post('http://localhost:8080/account/change_password', {
      username: username.value,
      oldPassword: currentPassword.value,
      newPassword: newPassword.value,
      confirmPassword: confirmPassword.value
    }, {
      headers: {
        'Content-Type': 'application/json' // 确保使用 JSON 格式
      }
    });

    console.log(response.data); // 成功信息
    ElMessage.success('密码修改成功！');
    isEditingPassword.value = false; // 关闭对话框
    // 清空输入框
    username.value = '';
    currentPassword.value = '';
    newPassword.value = '';
    confirmPassword.value = '';
  } catch (error) {
    console.error('修改密码失败:', error.response.data); // 错误信息
    ElMessage.error('修改密码失败: ' + error.response.data);
  }
  onMounted(onSavePassword);
};



const currentPasswordVisible = ref(false);
const newPasswordVisible = ref(false);
const confirmPasswordVisible = ref(false);

const toggleCurrentPasswordVisibility = () => {
  currentPasswordVisible.value = !currentPasswordVisible.value;
};

const toggleNewPasswordVisibility = () => {
  newPasswordVisible.value = !newPasswordVisible.value;
};

const toggleConfirmPasswordVisibility = () => {
  confirmPasswordVisible.value = !confirmPasswordVisible.value;
};
</script>

<template>
  <div class="head">
    <div class="head-left">
      <span>
        <el-icon><Shop /></el-icon>
        {{ seller.storeName }}
      </span>
    </div>
    <div class="head-right">
    
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-avatar :size="50" :src="seller.avatarUrl" />
          <span>{{ seller.username }}</span>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="openPasswordDialog">修改密码</el-dropdown-item>
            <el-dropdown-item @click="logout" class="logout-button">注销账号</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <el-popconfirm @confirm="confirm" title="确认退出吗？" confirm-button-text="确认" cancel-button-text="取消">
        <template #reference>
          <a href="javascript:;">退出登录</a>
        </template>
      </el-popconfirm>
    </div>
    
    <el-dialog v-model="isEditingPassword" title="修改密码">
    <el-form label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="username"></el-input>
      </el-form-item>
      <el-form-item label="当前密码">
        <el-input
          :type="currentPasswordVisible ? 'text' : 'password'"
          v-model="currentPassword"
          :suffix-icon="currentPasswordVisible ? 'el-icon-view' : 'el-icon-view-off'"
          @click:suffix="toggleCurrentPasswordVisibility"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码">
        <el-input
          :type="newPasswordVisible ? 'text' : 'password'"
          v-model="newPassword"
          :suffix-icon="newPasswordVisible ? 'el-icon-view' : 'el-icon-view-off'"
          @click:suffix="toggleNewPasswordVisibility"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码">
        <el-input
          :type="confirmPasswordVisible ? 'text' : 'password'"
          v-model="confirmPassword"
          :suffix-icon="confirmPasswordVisible ? 'el-icon-view' : 'el-icon-view-off'"
          @click:suffix="toggleConfirmPasswordVisibility"
        ></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="isEditingPassword = false">取消修改</el-button>
      <el-button type="primary" @click="onSavePassword">确认修改</el-button>
    </template>
  </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.head {
  display: flex;
  align-items: center;
  justify-content: space-between; /* 左右分布 */
  padding: 10px 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 100%; /* 占满整个宽度 */
}

/* 左侧部分样式 */
.head-left {
  display: flex;
  align-items: center;
}

.head-left span {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.head-left .el-icon {
  margin-right: 8px;
  color: #333;
}

/* 右侧部分样式 */
.head-right {
  display: flex;
  align-items: center;
  gap: 20px; /* 设置右侧图标和下拉菜单之间的间距 */
}

.head-right .el-icon {
  cursor: pointer;
}

/* 下拉菜单样式 */
.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
  gap: 20px;
}

.el-dropdown-link .el-icon--right {
  margin-left: 4px; /* 下拉箭头与文本之间的间距 */
}

.el-avatar {
  border: 1px solid #eee; /* 头像边框 */
}

.el-dropdown-menu {
  font-size: 14px;
}
</style>
